package com.jiyun.controller;

import lombok.extern.slf4j.Slf4j;
import org.apache.kafka.clients.consumer.ConsumerRecord;
import org.springframework.kafka.annotation.KafkaListener;
import org.springframework.stereotype.Component;

import java.util.Optional;

/**
 * @projectName: 2210A-project1
 * @package: com.jiyun.controller
 * @className: KafkaListen
 * @author: 耿嘉军
 * @date: 2023/9/9 16:22
 * @version: 1.0
 */
@Component
@Slf4j
public class KafkaListen {

    @KafkaListener(topics = {"kafka-hello"})
    public void receiverMessage(ConsumerRecord<?,?> record){
        try {
            log.info("进入kafka的消费模式：");
            Optional<? extends ConsumerRecord<?, ?>> optional = Optional.ofNullable(record);
            if(optional.isPresent()){
                Object value = record.value();
                System.out.println(value);
            }
        } catch (Exception e) {
            e.printStackTrace();
        }
    }

}
